<?php
/*
 * @Created by: wangchengcheng
 * @Author: wangchengcheng
 * @Date: 2020-01-10 16:19:52
 * @LastEditors  : wangchengcheng
 * @LastEditTime : 2020-01-13 09:34:53
 */

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\Site\Store;
use App\Models\Site;
use App\Models\SocialiteUser;
use App\Notifications\SiteApply;
use Notification;

class SiteController extends Controller
{
    public function index()
    {
        $site = Site::select('id', 'name', 'url', 'description')
            ->where('audit', 1)
            ->orderBy('sort')
            ->get();
        $head = [
            'title'         => __('Recommend Blog'),
            'keywords'      => __('Recommend Blog'),
            'description'   =>__('Recommend Blog'),
        ];
        $assign = [
            'site'          => $site,
            'head'          => $head,
            'category_id'   => 'index',
            'tagName'       => '',
        ];

        return view('home.site.index', $assign);
    }

    public function store(Store $request)
    {
        $socialiteUserId = auth()->guard('socialite')->user()->id;

        $siteData                           = $request->only('name', 'url', 'description');
        $siteData['socialite_user_id']      = $socialiteUserId;
        $sort                               = Site::orderBy('sort', 'desc')->value('sort');
        $siteData['sort']                   = (int) $sort + 1;
        $result                             = Site::create($siteData);
        
        if($result){
            SocialiteUser::where('id', $socialiteUserId)->update([
                'email' => $request->input('email'),
            ]);

            Notification::route('email', config('wblog.notification_email'))
                ->notify(new SiteApply());

            return response()->json();
        }else{
            return response()->json('', 500);
        }
    }
}
